package com.woniu.controller;

import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryUserDTO;
import com.woniu.entity.vo.OrderByIdVO;
import com.woniu.entity.vo.QueryUserVO;
import com.woniu.entity.vo.UserDetailedStatisticsVO;
import com.woniu.pojo.User;
import com.woniu.service.UserService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @ApiOperation("分页+搜索")
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryUserDTO queryUserDTO){
        PageData pageData = userService.listSearch(currentPage, pageSize, queryUserDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("用户禁用")
    @PostMapping("/disabledUser/{id}/{status}")
    public Result disabledUser(@PathVariable String id,@PathVariable Integer status){
        int i = userService.disabledUser(id, status);
        if (i>0) {
            return success("操作成功");
        }else {
            return error("操作失败");
        }
    }

    @ApiOperation("根据ID查询用户")
    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable String id){
        User user = userService.getById(id);
        return toDataResult(user);
    }


    /** 用户详情使用到此API */
    @ApiOperation("根据用户ID查询订单表")
    @GetMapping("/getOrderRecordByUserId/{id}")
    public Result getOrderRecordByUserId(@PathVariable String id){
        List<OrderByIdVO> userOrdeer = userService.getOrderRecordByUserId(id);
        return toDataResult(userOrdeer);
    }

    @ApiOperation("根据用户ID统计信息")
    @GetMapping("/getDetailedStatisticsByUserId/{id}")
    public Result getDetailedStatisticsByUserId(@PathVariable String id){
        UserDetailedStatisticsVO userDetailedStatisticsVO = userService.getDetailedStatisticsByUserId(id);
        return toDataResult(userDetailedStatisticsVO);
    }


}
